package form;

import java.util.List;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import entity.Customer;
import session.CustomerFacade;

public final class OrderForm
{
    private static void cardNumberIsValid(String cardNumber) throws FormValidationException
    {		
        if(cardNumber != null)
        {
            Integer cn = null;
            try
            {
                cn = Integer.parseInt(cardNumber);             
            }
            catch(Exception e)
            {
            }
            if(cn == null)
            {
                throw new FormValidationException("Thanks to type a valid credit card number with figures.");                
            }
            else if(cardNumber.length() == 16)
            {
                throw new FormValidationException("Thanks to type a valid credit card number with 16 figures.");                
            }
        } 
        else
        {
                throw new FormValidationException("Thanks to type a credit card number.");
        }
    }
	
    private static void cryptogramIsValid(String cryptogram) throws FormValidationException
    {		
        if(cryptogram != null)
        {
            Integer c = null;
            try
            {
                c = Integer.parseInt(cryptogram);             
            }
            catch(Exception e)
            {
            }
            if(c == null)
            {
                throw new FormValidationException("Thanks to type a valid cryptogram with figures.");                
            }
            else if(cryptogram.length() == 3)
            {
                throw new FormValidationException("Thanks to type a valid cryptogram with 3 figures.");                
            }
        } 
        else
        {
                throw new FormValidationException("Thanks to type a cryptogram.");
        }
    }
        
    public static List<String> validateForm(HttpServletRequest request)
    {
        List<String> errors = new ArrayList<>();
        
        try
        {
            cardNumberIsValid(request.getParameter("cardnumber"));
        }
        catch(FormValidationException fve)
        {
            errors.add(fve.getMessage());
        }
        
        try
        {
            cryptogramIsValid(request.getParameter("cryptogram"));
        }
        catch(FormValidationException fve)
        {
            errors.add(fve.getMessage());
        }
        
        return errors;
    }
}